package cn.emay;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.emay.eucp.inter.framework.dto.CustomSmsIdAndMobile;
import cn.emay.eucp.inter.framework.dto.CustomSmsIdAndMobileAndContent;
import cn.emay.eucp.inter.http.v1.dto.request.BalanceRequest;
import cn.emay.eucp.inter.http.v1.dto.request.MoRequest;
import cn.emay.eucp.inter.http.v1.dto.request.ReportRequest;
import cn.emay.eucp.inter.http.v1.dto.request.SmsBatchOnlyRequest;
import cn.emay.eucp.inter.http.v1.dto.request.SmsBatchRequest;
import cn.emay.eucp.inter.http.v1.dto.request.SmsPersonalityRequest;
import cn.emay.eucp.inter.http.v1.dto.request.SmsSingleRequest;
import cn.emay.eucp.inter.http.v1.dto.response.BalanceResponse;
import cn.emay.eucp.inter.http.v1.dto.response.MoResponse;
import cn.emay.eucp.inter.http.v1.dto.response.ReportResponse;
import cn.emay.eucp.inter.http.v1.dto.response.SmsResponse;
import cn.emay.util.AES;
import cn.emay.util.GZIPUtils;
import cn.emay.util.JsonHelper;
import cn.emay.util.SendMessageUtils;
import cn.emay.util.http.EmayHttpClient;
import cn.emay.util.http.EmayHttpRequestBytes;
import cn.emay.util.http.EmayHttpResponseBytes;
import cn.emay.util.http.EmayHttpResponseBytesPraser;
import cn.emay.util.http.EmayHttpResultCode;
import com.yunfei.utils.DateUtil;

public class Example {

	// appId用户序列号: EUCP-EMY-SMS1-5DDK5  密码和key都设置为: 89D9E0  特服号: 061274   附件是接口包请查看!
	private static String appId = "EUCP-EMY-SMS1-5DDK5";
	// 密钥
	private static String secretKey = "89D9E05BBFF4DFAA";
	// 接口地址
	private static String host = "bjmtn.b2m.cn";
	// 加密算法
	private static String algorithm = "AES";

	public static void main(String[] args) {
		SendMessageUtils.setSingleSms("【秀实投资APP】亲爱的秀实投资理财师毕芳，产品秀实合融2号到期日由2019-10-16变更为2029-10-16,请知悉。","13717625140",true);
		// 获取余额
		//getBalance(false);
		// 发送单条短信
	//setSingleSms("【秀实投资集团|耀莱】你的验证码是8989", null, null, "17611255968",false);
		//setBatchOnlySms("【秀实投资集团|耀莱】你的验证码是8989", null, new String[]{"17611255968"},false,"2018-04-10 18:50:00");
	/*		// 发送批次短信[有自定义SMSID]
		setBatchSms("你好今天天气不错，挺风和日丽的", null, new CustomSmsIdAndMobile[]{new CustomSmsIdAndMobile("1", "15538853919"),new CustomSmsIdAndMobile("2", "15538853929")},false);
		// 发送批次短信[无自定义SMSID]
		setBatchOnlySms("你好今天天气不错，挺风和日丽的", null, new String[]{"15538853919","15538853627"},false);
		// 发送个性短信
		setPersonalitySms( null, new CustomSmsIdAndMobileAndContent[]{new CustomSmsIdAndMobileAndContent("1", "15538853919","你好今天天气不错，挺风和日丽的"),new CustomSmsIdAndMobileAndContent("2", "15538853929","你好今天天气不错，挺风和日丽的啊")},false);
		// 获取状态报告
		getReport(false);
		// 获取上行
		getMo(false);*/
	}
	
	/**
	 * 获取余额
	 * @param isGzip 是否压缩
	 */
	private static void getBalance(boolean isGzip) {
		System.out.println("=============begin getBalance==================");
		BalanceRequest pamars = new BalanceRequest();
		ResultModel result = request(pamars, "http://" + host + "/inter/getBalance",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			BalanceResponse response = JsonHelper.fromJson(BalanceResponse.class, result.getResult());
			if (response != null) {
				System.out.println("result data : " + response.getBalance());
			}
		}
		System.out.println("=============end getBalance==================");
	}

	/**
	 * 获取状态报告
	 * @param isGzip 是否压缩
	 */
	private static void getReport(boolean isGzip) {
		System.out.println("=============begin getReport==================");
		ReportRequest pamars = new ReportRequest();
		ResultModel result = request(pamars, "http://" + host + "/inter/getReport",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			ReportResponse[] response = JsonHelper.fromJson(ReportResponse[].class, result.getResult());
			if (response != null) {
				for (ReportResponse d : response) {
					System.out.println("result data : " + d.getMobile() + "," + d.getExtendedCode() + "," + d.getMobile() + "," + d.getCustomSmsId() + "," + d.getSmsId() + "," + d.getState() + "," + d.getDesc()
							+ "," + d.getSubmitTime() + "," + d.getReceiveTime());
				}
			}
		}
		System.out.println("=============end getReport==================");
	}
	
	/**
	 * 获取上行
	 * @param isGzip 是否压缩
	 */
	private static void getMo(boolean isGzip) {
		System.out.println("=============begin getMo==================");
		MoRequest pamars = new MoRequest();
		ResultModel result = request(pamars, "http://" + host + "/inter/getMo",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			MoResponse[] response = JsonHelper.fromJson(MoResponse[].class, result.getResult());
			if (response != null) {
				for (MoResponse d : response) {
					System.out.println("result data:" + d.getMobile() + "," + d.getExtendedCode() + "," + d.getMobile() + "," + d.getMoTime());
				}
			}
		}
		System.out.println("=============end getMo==================");
	}
	
	/**
	 * 发送单条短信
	 * @param isGzip 是否压缩
	 */
	private static void setSingleSms(String content, String customSmsId, String extendCode, String mobile,boolean isGzip) {
		System.out.println("=============begin setSingleSms==================");
		SmsSingleRequest pamars = new SmsSingleRequest();
		pamars.setContent(content);
		pamars.setCustomSmsId(customSmsId);
		pamars.setExtendedCode(extendCode);
		pamars.setMobile(mobile);
		ResultModel result = request(pamars, "http://" + host + "/inter/sendSingleSMS",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			SmsResponse response = JsonHelper.fromJson(SmsResponse.class, result.getResult());
			if (response != null) {
				System.out.println("data : " + response.getMobile() + "," + response.getSmsId() + "," + response.getCustomSmsId());
			}
		}
		System.out.println("=============end setSingleSms==================");
	}
	
	/**
	 * 发送批次短信
	 * @param isGzip 是否压缩
	 */
	private static void setBatchOnlySms(String content, String extendCode, String[] mobiles,boolean isGzip,String timeerTime) {
		System.out.println("=============begin setBatchOnlySms==================");
		SmsBatchOnlyRequest pamars = new SmsBatchOnlyRequest();
		pamars.setMobiles(mobiles);
		pamars.setExtendedCode(extendCode);
		pamars.setContent(content);
		Date dateTime= DateUtil.parseDate(timeerTime);
		pamars.setTimerTime(dateTime);
		ResultModel result = request(pamars, "http://" + host + "/inter/sendBatchOnlySMS",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			SmsResponse[] response = JsonHelper.fromJson(SmsResponse[].class, result.getResult());
			if (response != null) {
				for (SmsResponse d : response) {
					System.out.println("data:" + d.getMobile() + "," + d.getSmsId() + "," + d.getCustomSmsId());
				}
			}
		}
		System.out.println("=============end setBatchOnlySms==================");
	}
	
	/**
	 * 发送批次短信
	 * @param isGzip 是否压缩
	 */
	private static void setBatchSms(String content, String extendCode, CustomSmsIdAndMobile[] customSmsIdAndMobiles,boolean isGzip) {
		System.out.println("=============begin setBatchSms==================");
		SmsBatchRequest pamars = new SmsBatchRequest();
		pamars.setSmses(customSmsIdAndMobiles);
		pamars.setExtendedCode(extendCode);
		pamars.setContent(content);
		ResultModel result = request(pamars, "http://" + host + "/inter/sendBatchSMS",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			SmsResponse[] response = JsonHelper.fromJson(SmsResponse[].class, result.getResult());
			if (response != null) {
				for (SmsResponse d : response) {
					System.out.println("data:" + d.getMobile() + "," + d.getSmsId() + "," + d.getCustomSmsId());
				}
			}
		}
		System.out.println("=============end setBatchSms==================");
	}
	
	/**
	 * 发送个性短信
	 * @param isGzip 是否压缩
	 */
	private static void setPersonalitySms(String extendCode, CustomSmsIdAndMobileAndContent[] customSmsIdAndMobileAndContents,boolean isGzip) {
		System.out.println("=============begin setPersonalitySms==================");
		SmsPersonalityRequest pamars = new SmsPersonalityRequest();
		pamars.setSmses(customSmsIdAndMobileAndContents);
		pamars.setExtendedCode(extendCode);
		ResultModel result = request(pamars, "http://" + host + "/inter/sendPersonalitySMS",isGzip);
		System.out.println("result code :" + result.getCode());
		if("SUCCESS".equals(result.getCode())){
			SmsResponse[] response = JsonHelper.fromJson(SmsResponse[].class, result.getResult());
			if (response != null) {
				for (SmsResponse d : response) {
					System.out.println("data:" + d.getMobile() + "," + d.getSmsId() + "," + d.getCustomSmsId());
				}
			}
		}
		System.out.println("=============end setPersonalitySms==================");
	}
	
	/**
	 * 公共请求方法
	 */
	public static ResultModel request(Object content, String url,final boolean isGzip) {
		EmayHttpRequestBytes request = null;
		Map<String, String> headers = new HashMap<String, String>();
		try {
			headers.put("appId", appId);
			String requestJson = JsonHelper.toJsonString(content);
			System.out.println("result json: " + requestJson);
			byte[] bytes = requestJson.getBytes(StandardCharsets.UTF_8);
			System.out.println("request data size : " + bytes.length);
			if (isGzip) {
				headers.put("gzip", "on");
				bytes = GZIPUtils.compress(bytes);
				System.out.println("request data size [com]: " + bytes.length);
			}
			byte[] parambytes = AES.encrypt(bytes, secretKey.getBytes(StandardCharsets.UTF_8), algorithm);
			System.out.println("request data size [en] : " + parambytes.length);
			request = new EmayHttpRequestBytes(url, "UTF-8", "POST", headers, null, parambytes);
		} catch (Exception e) {
			System.out.println("加密异常");
			e.printStackTrace();
		}
		EmayHttpClient client = new EmayHttpClient();
		String code = null;
		String result = null;
		try {
			EmayHttpResponseBytes res = client.service(request, new EmayHttpResponseBytesPraser());
			if (res.getResultCode().equals(EmayHttpResultCode.SUCCESS)) {
				if (res.getHttpCode() == 200) {
					code = res.getHeaders().get("result");
					if (code.equals("SUCCESS")) {
						byte[] data = res.getResultBytes();
						System.out.println("response data size [en and com] : " + data.length);
						data = AES.decrypt(data, secretKey.getBytes(StandardCharsets.UTF_8), algorithm);
						if (isGzip) {
							data = GZIPUtils.decompress(data);
						}
						System.out.println("response data size : " + data.length);
						result = new String(data, StandardCharsets.UTF_8);
						System.out.println("response json: " + result);
					}
				} else {
					System.out.println("请求接口异常,请求码:" + res.getHttpCode());
				}
			} else {
				System.out.println("请求接口网络异常:" + res.getResultCode().getCode());
			}
		} catch (Exception e) {
			System.out.println("解析失败");
			e.printStackTrace();
		}
		ResultModel re = new ResultModel(code, result);
		return re;
	}

}
